<?php


namespace api\blog\controller;


use cmf\controller\RestBaseController;

class WuliuController  extends RestBaseController
{
//    public function wuliu(){
//        $used = "1855249";//仅作为示例ID，不可用来实际使用
//    //加密私钥，由快递鸟提供
//        $keyValue = "6434c4a0-b270-4719-a77a-373e3794af5e";//仅作为示例Key，不可用来实际使用
//    //请求地址
//        $url = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
//    //2-json
//        $DataType = "2";
//    //字符编码采用UTF-8
//        $charset = "UTF-8";
//    //JSON字符串string
//        $jsonStr = [
//            'OrderCode' => '',
//            'ShipperCode' => 'YTO',
//            'LogisticCode' => 'YT2253890514882'
//        ];
//    //把(jsonStr+APIKey)进行MD5加密，然后Base64编码，最后 进行URL(utf-8)编码
//        $datasign = urlencode(base64_encode(md5($jsonStr.$keyValue, $charset),$charset), Encoding.UTF8);
//    //请求报文参数
//        $PostStr = "RequestType=8001 & EBusinessID= $used &RequestData=$jsonStr &DataSign= $datasign&DataType=$DataType";
//    //通讯协议使用Https协议Post请求方式
//        $post = $this->DoPost($url, $PostStr);
//    }
    public function wuliu()
    {
        $url = 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
        $RequestData = [
            'LogisticCode' => '777221640641694'
        ];
        $apiKey = '6434c4a0-b270-4719-a77a-373e3794af5e';
        $RequestDataJson = json_encode($RequestData);
        $DataSign = urlencode(base64_encode(md5($RequestDataJson.$apiKey)));

        //其他参数
        $param = [
            'RequestData' => $RequestDataJson,
            'RequestType' => 8002,
            'EBusinessID' => 1855249,
            'DataSign' => $DataSign,
            'DataType' => 2
        ];
        $res = $this->curlRequest('POST',$url,'form',$param);
        $this->success('查询成功',$res);


    }
    // CURL
    function curlRequest($type, $url, $contentType, $data = [], $timeout = 60)
    {
        try {
            $type = strtoupper($type);
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url); // 设置请求链接
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 不直接输出页面
            curl_setopt($curl, CURLOPT_HEADER, 0); // 获取响应头向下
            curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);   // 请求超时时间，单位：秒
            curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1');
            if (substr($url, 0, 5) == 'https') {
                // 自动判断是否是 https 提交
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
                // https请求，不验证证书和 hosts
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            }
            // 判断 Content-Type 类型
            if ($contentType == 'json') {
                curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
            }
            if ($contentType == 'form') {
                curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/x-www-form-urlencoded']);
            }
            switch ($type) {
                case "GET" :
                    curl_setopt($curl, CURLOPT_HTTPGET, true);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                    break;
                case "POST":
                    curl_setopt($curl, CURLOPT_POST, true);
                    if ($contentType == 'json') {
                        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
                    }
                    if ($contentType == 'form') {
                        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
                    }
                    break;
            }
            $result = curl_exec($curl);
            $err_code = curl_errno($curl);
            $curlInfo = curl_getinfo($curl);
            curl_close($curl);
            if ($err_code) {
                return ['status' => 'error', 'msg' => 'sendRequestError:' . $err_code];
            }
            if ($curlInfo['http_code'] == 200) {
                return json_decode($result, true);
            }
            return false;
        } catch (\Exception $e) {
            return ['status' => 'error', 'msg' => $e->getMessage()];
        }
    }
}